#!/bin/sh

# docker run --rm -v $PWD:/var/task lambci/lambda:provided handler '{"some": "event"}'

while true
do
  HEADERS="$(mktemp)"

  EVENT_DATA=$(curl -v -sS -LD "$HEADERS" -X GET "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/next")
  INVOCATION_ID=$(grep -Fi Lambda-Runtime-Aws-Request-Id "$HEADERS" | tr -d '[:space:]' | cut -d: -f2)

  echo "Invoke received. Request ID: ${INVOCATION_ID}. Event:"
  echo $EVENT_DATA

  RESPONSE="Hello from Lambda!"

  curl -v "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/$INVOCATION_ID/response" -d "$RESPONSE"
done
